home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 1.iso / toolbox / src / tutorials / custEducation / opengl1 / examples / depth_buffer / cullface.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-11-11  |  3.8 KB  |  166 lines

  1. /* Copyright 1993, 1994, 1996 Silicon Graphics, Inc.
  2.  * All Rights Reserved.
  3.  *
  4.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  5.  * the contents of this file may not be disclosed to third parties, copied or
  6.  * duplicated in any form, in whole or in part, without the prior written
  7.  * permission of Silicon Graphics, Inc.
  8.  *
  9.  * RESTRICTED RIGHTS LEGEND:
  10.  * Use, duplication or disclosure by the Government is subject to restrictions
  11.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  12.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  13.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  14.  * rights reserved under the Copyright Laws of the United States.
  15.  */
  16.  
  17. /* cullface.c - draw a solid torus with front/back face culling.
  18.  *
  19.  *    <f> Key        - toggle between front/back face culling
  20.  *    <r> Key        - rotate polygon along X axis
  21.  *    Escape Key    - exit program
  22.  */
  23. #include <GL/gl.h>
  24. #include <GL/glu.h>
  25. #include <GL/glut.h>
  26.  
  27. #include <stdio.h>
  28.  
  29. /*  Function Prototypes  */
  30.  
  31. GLvoid  initgfx( GLvoid );
  32. GLvoid  keyboard( GLubyte, GLint, GLint );
  33. GLvoid  drawScene( GLvoid );
  34. GLvoid  reshape( GLsizei, GLsizei );
  35.  
  36. void printHelp( char * );
  37.  
  38. /* Global Definitions */
  39.  
  40. #define KEY_ESC    27    /* ascii value for the escape key */
  41.  
  42. /* Global Variables */
  43.  
  44. static GLint     cullface = GL_BACK;
  45. static GLboolean doRotate = GL_FALSE;
  46.  
  47. void
  48. main( int argc, char *argv[] )
  49. {
  50.     GLsizei width, height;
  51.  
  52.     glutInit( &argc, argv );
  53.  
  54.     width = glutGet( GLUT_SCREEN_WIDTH ); 
  55.     height = glutGet( GLUT_SCREEN_HEIGHT );
  56.     glutInitWindowPosition( (width / 2) + 4, height / 4 );
  57.     glutInitWindowSize( (width / 2) - 4, height / 2 );
  58.     glutInitDisplayMode( GLUT_RGBA | GLUT_DEPTH );
  59.     glutCreateWindow( argv[0] );
  60.  
  61.     initgfx();
  62.  
  63.     glutReshapeFunc( reshape );
  64.     glutKeyboardFunc( keyboard );
  65.     glutDisplayFunc( drawScene ); 
  66.  
  67.     printHelp( argv[0] );
  68.  
  69.     glutMainLoop();
  70. }
  71.  
  72. void
  73. printHelp( char *progname )
  74. {
  75.     fprintf(stdout, "\n%s - demonstrates depth buffering\n\n"
  76.         "<f> Key    - toggle between front/back face culling\n"
  77.         "<r> Key    - rotate polygon around X axis\n"
  78.         "Escape Key    - exit the program\n\n",
  79.         progname);
  80.  
  81.     fprintf(stdout, "culling %s faces\n", 
  82.         (cullface == GL_FRONT ? "FRONT" : "BACK" )); 
  83. }
  84.  
  85. GLvoid
  86. initgfx( GLvoid )
  87. {
  88.     glClearColor( 0.0, 0.0, 1.0, 1.0 );
  89.     glShadeModel( GL_FLAT );
  90.  
  91.     glEnable( GL_DEPTH_TEST );
  92.  
  93.     /* Enable face culling */
  94.     glEnable( GL_CULL_FACE );
  95. }
  96.  
  97. GLvoid
  98. reshape( GLsizei width, GLsizei height )
  99. {
  100.     GLdouble    aspect;
  101.  
  102.     glViewport( 0, 0, width, height );
  103.  
  104.     glMatrixMode( GL_PROJECTION );
  105.     glLoadIdentity();
  106.     aspect = (GLdouble) width / (GLdouble) height;
  107.     gluPerspective( 45.0, aspect, 3.0, 7.0 );
  108.     glMatrixMode( GL_MODELVIEW );
  109. }
  110.  
  111. GLvoid 
  112. keyboard( GLubyte key, GLint x, GLint y )
  113. {
  114.     switch (key) {
  115.     case 'f':    /* toggle between front/back face culling */
  116.         if (cullface == GL_FRONT) {
  117.             cullface = GL_BACK;
  118.         } else {
  119.             cullface = GL_FRONT;
  120.         }
  121.         fprintf(stdout, "culling %s faces\n", 
  122.             (cullface == GL_FRONT ? "FRONT" : "BACK" )); 
  123.         glCullFace( cullface );
  124.         glutPostRedisplay();
  125.         break;
  126.     case 'r':
  127.         doRotate = !doRotate;
  128.         glutPostRedisplay();
  129.         break;
  130.     case KEY_ESC:    /* Exit when the Escape key is pressed */
  131.         exit(0);
  132.     }
  133. }
  134.  
  135. GLvoid
  136. drawScene( GLvoid )
  137. {
  138.     static GLfloat    whiteColor[] = { 1.0, 1.0, 1.0 };
  139.     static GLfloat    purpleColor[] = { 0.8, 0.0, 1.0 };
  140.  
  141.     glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
  142.  
  143.     glPushMatrix();
  144.         /* Move origin between the near and far clipping planes */
  145.         glTranslatef( 0.0, 0.0, -4.0 );
  146.  
  147.         /* Draw a grid in the x-y plane centered at the origin */
  148.         glColor3fv( whiteColor );
  149.         WireGrid();
  150.  
  151.         glColor3fv( purpleColor );
  152.  
  153.         /* Draw a rectangle in front of the grid */
  154.         glTranslatef( 0.0, 0.0, 0.1 );
  155.  
  156.         if (doRotate)
  157.             glRotatef( -45.0, 1.0, 0.0, 0.0 );
  158.  
  159.         /* Draw a torus */
  160.         glutSolidTorus( 0.2, 0.7, 15, 31 );
  161.  
  162.     glPopMatrix();
  163.  
  164.     glFlush();
  165. }
  166.